package covertjava.protect;
import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import sun.misc.BASE64Encoder;

/**
 * <p>Generates license strings based on license parameters.
 * Uses a private key of asymmetric encryption algorithm.</p>
 * <p>Copyright: Copyright (c) 2004 Sams Publishing</p>
 * @author Alex Kalinovsky
 * @version 1.0
 */
public class LicenseGenerator extends LicenseManager
{

    public LicenseGenerator(String licenseFileName) throws Exception {
        super(licenseFileName);
    }

    public void generateKeys() throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        random.setSeed(System.currentTimeMillis());
        keyGen.initialize(1024, random);
        KeyPair pair = keyGen.generateKeyPair();

        String publicKeyPath = home + File.separator + "conf" + File.separator + "key_public.ser";
        byte[] bytes = pair.getPublic().getEncoded();
        FileOutputStream stream = new FileOutputStream(publicKeyPath);
        stream.write(bytes);
        stream.close();

        String privateKeyPath = home + File.separator + "conf" + File.separator + "key_private.ser";
        bytes = pair.getPrivate().getEncoded();
        stream = new FileOutputStream(privateKeyPath);
        stream.write(bytes);
        stream.close();

        System.out.println("Keys have been generated and saved in files " + publicKeyPath + " and " + privateKeyPath);
    }

    public String generateSerialNumber() throws Exception {
        String licenseString = getLicenseString();
        byte[] serialBytes = licenseString.getBytes(CHARSET);
        serialBytes = getSignature(serialBytes);
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(serialBytes);
    }


    private byte[] getSignature(byte[] serialBytes) throws Exception {
        String privateKeyPath = home + File.separator + "conf" + File.separator + "key_private.ser";
        FileInputStream stream = new FileInputStream(privateKeyPath);
        byte[] encodedPrivateKey = new byte[stream.available()];
        stream.read(encodedPrivateKey);

        PKCS8EncodedKeySpec pubKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        PrivateKey key = keyFactory.generatePrivate(pubKeySpec);

        Signature dsa = Signature.getInstance("SHA1withDSA");
        dsa.initSign(key);
        dsa.update(serialBytes);
        return dsa.sign();
    }


    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.out.println("Syntax: LicenseGenerator -<command> <license file>");
            System.out.println("where command = keys | serial");
            System.exit(1);
        }

        LicenseGenerator generator = new LicenseGenerator(args[1]);

        if ("-keys".equals(args[0])) {
            generator.generateKeys();
        }
        else if("-serial".equals(args[0])) {
            String serial = generator.generateSerialNumber();
            System.out.println("Serial number = [" + serial + "]");
        }
        else {
            System.err.println("Unknown command " + args[0]);
            System.exit(1);
        }
    }

}



